package com.yisoo.ubag.iot.service.impl;

import com.yisoo.common.utils.R;
import com.yisoo.ubag.iot.entity.DeviceEntity;
import com.yisoo.ubag.iot.service.DeviceService;
import com.yisoo.ubag.iot.vo.AckMqttContentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.Query;

import com.yisoo.ubag.iot.dao.BoxRelBagDao;
import com.yisoo.ubag.iot.entity.BoxRelBagEntity;
import com.yisoo.ubag.iot.service.BoxRelBagService;


@Service("boxRelBagService")
public class BoxRelBagServiceImpl extends ServiceImpl<BoxRelBagDao, BoxRelBagEntity> implements BoxRelBagService {

    @Autowired
    DeviceService deviceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BoxRelBagEntity> page = this.page(
                new Query<BoxRelBagEntity>().getPage(params),
                new QueryWrapper<BoxRelBagEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public Integer boxIsHaveBag(Long devId) {

        BoxRelBagEntity entity = baseMapper.selectById(devId);
        if (entity == null){
            return -1;
        }else{
            return entity.getBagId().intValue();
        }


    }

    @Override
    public Long getEntityByBagId(String bagId) {

        QueryWrapper<BoxRelBagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("bag_id",bagId);
        return baseMapper.selectOne(queryWrapper).getBoxId();
    }

    @Override
    public void updateByAckMqttContentVo(AckMqttContentVo content) {
        QueryWrapper<BoxRelBagEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("box_name",content.getDeviceName());
        BoxRelBagEntity boxRelBagEntity = baseMapper.selectOne(queryWrapper);
        if (boxRelBagEntity == null){
            DeviceEntity box = deviceService.getDevByName(content.getDeviceName());
            DeviceEntity bag = deviceService.getDevByName(content.getMsg());
            BoxRelBagEntity entity = new BoxRelBagEntity(box.getDevId(), box.getDeviceName(), bag.getDevId(), bag.getDeviceName());

            baseMapper.insert(entity);
        }else{
            DeviceEntity bag = deviceService.getDevByName(content.getMsg());
            boxRelBagEntity.setBagId(bag.getDevId());
            boxRelBagEntity.setBagName(bag.getDeviceName());
            baseMapper.updateById(boxRelBagEntity);
        }
    }
    @Override
    public R lock(String devcode, Long bagId) {
        DeviceEntity box = deviceService.getDevBySecret(devcode);
        DeviceEntity bag = deviceService.getById(bagId);
        BoxRelBagEntity boxRelBagEntity = new BoxRelBagEntity(box.getDevId(), box.getDeviceName(), bag.getDevId(), bag.getDeviceName());
        baseMapper.updateById(boxRelBagEntity);

        deviceService.testMapping(box.getDevId(),102);

        return R.ok();

    }
}